package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.*;
import com.JZ.QM.common.entity.*;
import com.JZ.QM.dto.ProductRatioDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.CombinationShareService;
import javax.annotation.Resource;



/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Slf4j
@Service("combanationShareService")
public class CombinationShareServiceImpl extends ServiceImpl<CombinationShareDao, CombinationShareEntity> implements CombinationShareService {
    @Resource
    CombinationShareDao combinationShareDao;
    @Resource
    ProductLibraryInfoDao productLibraryInfoDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<CombinationShareEntity> page = this.page(
                new Query<CombinationShareEntity>().getPage(params),
                new QueryWrapper<CombinationShareEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过组合id获取组合的详情信息
     * @param comBaseId 组合基本信息id
     * @return
     */
    @Override
    public String getCombinationDetail(Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //备选库条件构造器
        QueryWrapper queryWrapperPro=new QueryWrapper();

        ProductLibraryInfoEntity productLibraryInfoEntities=new ProductLibraryInfoEntity();
        //组合产品信息列表
        ArrayList<CombinationShareEntity> combinationShareEntities=new ArrayList<>();
        //组合相信信息字符串
        String result=new String();
        //组合产品基本信息
        CombinationShareEntity combinationShareEntity=new CombinationShareEntity();
        //产品代码列表
        List<String> proCodes= new ArrayList();
        //将bigdecimal转换为字符串
        DecimalFormat df = new DecimalFormat("0.00%");

        queryWrapper.select("pro_ts_code","com_share_radio")
                .eq("com_base_id",comBaseId);

        combinationShareEntities= (ArrayList<CombinationShareEntity>) combinationShareDao.selectList(queryWrapper);

        for (Iterator<CombinationShareEntity> it = combinationShareEntities.iterator(); it.hasNext(); ) {
            combinationShareEntity= it.next();
            queryWrapperPro.select("pro_name")
                    .in("pro_ts_code",combinationShareEntity.getProTsCode());

            productLibraryInfoEntities=productLibraryInfoDao.selectOne(queryWrapperPro);

            result+=(productLibraryInfoEntities.getProName()+":"+df.format(combinationShareEntity.getComShareRadio())+";");

            queryWrapperPro.clear();
        }
        return result;
    }

    /**
     * 获取股票列表或基金列表
     * @param comBaseId
     * @param type 1 是股票   0 是基金
     * @return
     */
    @Override
    public ArrayList<CombinationShareEntity> getCombinationShareList(Long comBaseId,int type) {
        //所有的成分产品
        ArrayList<CombinationShareEntity> TotalList = new ArrayList<>();
        //符合条件的成分产品
        ArrayList<CombinationShareEntity> resultList = new ArrayList<>();
        //类型判断flag
        boolean typeFlag = true;
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("com_base_id",comBaseId);

        TotalList = (ArrayList<CombinationShareEntity>)  combinationShareDao.selectList(queryWrapper);


        for (CombinationShareEntity combinationShareEntity:TotalList) {
            typeFlag = verifyIsStock(combinationShareEntity.getProTsCode(),type);
            if(typeFlag){
                resultList.add(combinationShareEntity);
            }
        }
        return resultList;
    }

    /**
     * 判断成分产品是不是股票
     * @param proTsCode
     * @return
     */
    @Override
    public boolean verifyIsStock(String proTsCode,int type) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //备选库实体
        ProductLibraryInfoEntity productLibraryInfoEntity = new ProductLibraryInfoEntity();

        queryWrapper.eq("pro_ts_code",proTsCode);
        productLibraryInfoEntity = productLibraryInfoDao.selectOne(queryWrapper);

        if (productLibraryInfoEntity != null){
            if(productLibraryInfoEntity.getIsShare() == type){
                return true;
            }
        }
        return false;
    }
}